﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using System.Text;
using System.Web.SessionState;

namespace FortuneHeaven.Web
{
    /// <summary>
    /// ValidationCode 的摘要说明
    /// </summary>
    public class ValidationCode : IHttpHandler, IRequiresSessionState
    {
        public static string Name = "ValidateCode";
        public static string ChargeName = "ChargeCheck";
        public void ProcessRequest(HttpContext context)
        {
            if (string.IsNullOrEmpty(context.Request["charge"]))
            {
                ValidateCode(context, Name);
            }
            else
            {
                ValidateCode(context, ChargeName);
            }
        }

        private string DecToS26(int dec, string chars)
        {
            StringBuilder builder = new StringBuilder();
            while (dec > 0)
            {
                builder.Append(chars[dec % 26]);
                dec /= 26;
            }
            return builder.ToString();
        }


        public void ValidateCode(HttpContext contex,string storename)
        {
            const string chars = "ABCDEFGHAJKLMNAPQRSTUVWXYZ";
            Color[] m_Colors = new Color[] { Color.Black, Color.Red, Color.Green, Color.Blue, Color.Brown, Color.DarkMagenta, Color.DarkRed, Color.Indigo, Color.DarkBlue, Color.Maroon };
            Color BackgroundColor = Color.White;

            #region 随机码生成
            Random r = new Random();
            int num = r.Next(1000, 100000);
            string code = num.ToString();
        
            string s26 = DecToS26(num / 1000, chars).Substring(0, 1);
            code = code.Substring(code.Length - 4, 4);
            code = code.Insert(DateTime.Now.Millisecond % 5, s26);

            contex.Session[storename] = code;
            
            #endregion

            Color fcolor = m_Colors[DateTime.Now.Millisecond % 10];
            SolidBrush brush = new SolidBrush(fcolor);


            using (Bitmap image = new Bitmap(80, 22))
            {
                using (Graphics g = Graphics.FromImage(image))
                {
                    g.Clear(BackgroundColor);
                    g.DrawRectangle(new Pen(brush), 0, 0, 79, 21);
                    //#region 随机噪点生成
                    //for (int i = 0; i < image.Width * 2; i++)
                    //{
                    //    int x = r.Next(image.Width);
                    //    int y = r.Next(image.Height);
                    //    image.SetPixel(x, y, fcolor);
                    //}
                    //#endregion

                    for (int i = 0; i < 5; i++)
                    {
                        string c = code.Substring(i, 1);
                        int v = (int)c[0];
                        Font f = (v + DateTime.Now.Millisecond) % 2 == 0 ? new Font("Verdana", 14 - (DateTime.Now.Second % 2) / 2) : new Font("Verdana", 14 + (DateTime.Now.Second % 2) / 2, FontStyle.Italic);
                        g.DrawString(c, f, brush, 4 + i * 14 - DateTime.Now.Millisecond % 2, DateTime.Now.Second % 2);
                    }

                    using (MemoryStream ms = new MemoryStream())
                    {
                        image.Save(ms, ImageFormat.Gif);
                        contex.Response.ClearContent();
                        contex.Response.ContentType = "image/Gif";
                        contex.Response.Expires = -1;
                        contex.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
                        contex.Response.Cache.SetNoStore();
                        contex.Response.BinaryWrite(ms.ToArray());
                        contex.Response.End();
                    }
                }

            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}